//
//  ProjectDetailController.swift
//  QualityControlSystem
//
//  Created by Daniel Hu on 1/5/17.
//  Copyright © 2017 com.mask. All rights reserved.
//

import UIKit

class ProjectDetailController: UITableViewController {
    
    var data: [SubProject]?
    let titleView = UIView()
    let titleLabel = UILabel()
    let location = UILabel()
    let id: String
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureTitleView()
        
        tableView.backgroundColor = Color.bgGray
        tableView.register(SubProjectInfoCell.self, forCellReuseIdentifier: "foo")
        if User.shared.role != .worker {
            navigationItem.rightBarButtonItem = UIBarButtonItem(title: "施工图", style: .plain, target: self, action: #selector(workingPlan))
        }
        loadData()
    }
    
    init(id: String) {
        self.id = id
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configureTitleView() {
//        UINavigationItemButtonView
        for view in (navigationController?.navigationBar.subviews)! {
            let foo = String(describing: type(of: view))
            if foo == "UINavigationItemButtonView" {
                view.removeFromSuperview()
            }
        }
        navigationItem.backBarButtonItem = UIBarButtonItem(title: nil, style: .plain, target: nil, action: nil)
//        titleView.frame = CGRect(x: 0, y: 0, width: Screen.width, height: 30)
        title = "化工厂项目"
        // title
//        let title = UILabel()
//        titleView.addSubview(title)
//        title.snp.makeConstraints({make in
//            make.left.top.right.equalTo(titleView)
//            make.height.equalTo(20)
//        })
//        title.text = "化工厂项目"
//        title.textAlignment = .center
//        title.textColor = UIColor.white
//        
//        // location
//        let location = UILabel()
//        titleView.addSubview(location)
//        location.snp.makeConstraints({make in
//            make.left.bottom.right.equalTo(titleView)
//            make.height.equalTo(10)
//        })
//        location.textAlignment = .center
//        location.text = "四川省成都市"
//        location.font = UIFont.systemFont(ofSize: 12.0)
//        location.textColor = UIColor.white
//        
//        navigationItem.titleView = titleView

    }
    
    func loadData() {
        HTTPRequest.subProjectListWith(projectId: id) { response in
            self.data = response
            self.tableView.reloadData()
        }
    }
    
    func workingPlan() {
        let layout = UICollectionViewFlowLayout()
        let workingPlanVC = WorkingPlanController(collectionViewLayout: layout)
        navigationController?.pushViewController(workingPlanVC, animated: true)
    }
    
    //MARK: - UITableViewDataSource
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return data?.count ?? 0
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "foo", for: indexPath) as? SubProjectInfoCell
        cell?.data = data?[indexPath.section]
        return cell!
    }
    
    //MARK: - UITableViewDelegate
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return Constant.subProjectCellHeight
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView()
        return header
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let subproject = data?[indexPath.row]
        let vc = SubProjectDetailController(projectId: id, id: (subproject?.id)!)
        navigationController?.pushViewController(vc, animated: true)
    }
}
